﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Objects;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.EntityClient;
using System.Xml.Linq;


namespace PowerCalculation
{
    public partial class Form1 : Form
    {
        public static ElectricsRepository ElectricsRepository;

        public Form1()
        {
            ElectricsRepository = ElectricRepoStore.GetRepository();
            InitializeComponent();
            InitializeControls();
        }

        public void InitializeControls()
        {

            ElectronicsGroupBx.Enabled = true;
            LampsGroupBx.Enabled = false;
            ElectronicsRadioBtn.Checked = true;

            ResidentialComboBx.Items.AddRange(new object[] {"Жилое", "Нежилое"});
            ResidentialComboBx.SelectedItem = ResidentialComboBx.Items[0];

            FloorComboBx.Items.Add(1);
            FloorComboBx.SelectedItem = FloorComboBx.Items[0];

            EntranceComboBx.Items.Add(1);
            EntranceComboBx.SelectedItem = EntranceComboBx.Items[0];

            //var dictionaryWireSections = new Dictionary<int, string>();
            //ElectricsRepository.GetSections().ToList().ForEach(wiresec => dictionaryWireSections.Add(wiresec.SectionId,
            //                                                                                     (string.Format("{0} мм", wiresec.SectionValue))));

            //WireSectionСomboBx.DataSource = dictionaryWireSections.ToArray();
            //WireSectionСomboBx.DisplayMember = "Value";
            //WireSectionСomboBx.ValueMember = "Key";
            //WireSectionСomboBx.SelectedItem = WireSectionСomboBx.Items[0];

          
            var dictionaryWireTypes = new Dictionary<int, string>();
            ElectricsRepository.GetMaterials().ToList().ForEach(wiretype => dictionaryWireTypes.Add(wiretype.MaterialId,
                                                                                                 (string.Format("{0}", wiretype.MaterialName))));

            WireTypeComboBx.DataSource = dictionaryWireTypes.ToArray();
            WireTypeComboBx.DisplayMember = "Value";
            WireTypeComboBx.ValueMember = "Key";
            WireTypeComboBx.SelectedItem = WireTypeComboBx.Items[0];
            

            var dictionaryElectrics = new Dictionary<int,string>();
            var dictionaryLamps = new Dictionary<int, string>();

            ElectricsRepository.GetElectronics().ToList().ForEach(elec=> dictionaryElectrics.Add(elec.ElectronicsId,
                                                                                                 (string.Format("{0} {1} кВт", elec.ElectronicsDescription, elec.ElectronicsPower))));

            ElectronicsComboBx.DataSource = dictionaryElectrics.ToArray();
            ElectronicsComboBx.DisplayMember ="Value";
            ElectronicsComboBx.ValueMember = "Key";

            ElectricsRepository.GetLamps().ToList().ForEach(lamp => dictionaryLamps.Add(lamp.LampId,
                                                                                     (string.Format("{0} {1} кВт", lamp.LampDescription, lamp.LampPower))));

            LampsComboBx.DataSource = dictionaryLamps.ToArray();
            LampsComboBx.DisplayMember = "Value";
            LampsComboBx.ValueMember = "Key";


            RedioCore1.Checked = true;
        }


        private void FloorsCountUpDownBx_ValueChanged(object sender, EventArgs e)
        {
            if (FloorsCountUpDownBx.Value >= 1)
            {
                FloorComboBx.Items.Clear();
                var floorsArray = new object[(int) FloorsCountUpDownBx.Value];

                for (int index = 0; index < (int) FloorsCountUpDownBx.Value; index++)
                {
                    floorsArray[index] = (object) (index + 1);
                }

                FloorComboBx.Items.AddRange(floorsArray);
                FloorComboBx.SelectedItem = FloorComboBx.Items[0];
            }
        }

        private void EntranceUpDownBx_ValueChanged(object sender, EventArgs e)
        {
            if(EntranceUpDownBx.Value >= 1)
            {
                EntranceComboBx.Items.Clear();
                var entranceArray = new object[(int) EntranceUpDownBx.Value];

                for (int index = 0; index < (int)EntranceUpDownBx.Value; index++)
                {
                    entranceArray[index] = (object)(index + 1);
                }

                EntranceComboBx.Items.AddRange(entranceArray);
                
                EntranceComboBx.SelectedItem = EntranceComboBx.Items[0];
            }
        }

        private void AddConsumerBtn_Click(object sender, EventArgs e)
        {
            if (ElectronicsRadioBtn.Checked == true)
            {
                var count = ElectronicsCountUpDownBx.Value;
                var elec = ElectricsRepository.GetElectronics((int)ElectronicsComboBx.SelectedValue);

                int electronicsId = (int) ElectronicsComboBx.SelectedValue;

                var wireId =  ElectricsRepository.GetReglamentWireId(electronicsId);
                ElectricsListBx.Rows.Add(new object[] { elec.ElectronicsDescription, count.ToString(), elec.ElectronicsId, "e", wireId, elec.ElectronicsPower });
            }
            else
            {
                var count = LampsCountUpDownBx.Value;
                var lamp = ElectricsRepository.GetLamp((int) LampsComboBx.SelectedValue);
                ElectricsListBx.Rows.Add(new object[]
                                             {
                                                 lamp.LampDescription, count.ToString(), lamp.LampId, "l", null,
                                                 lamp.LampPower
                                             });
            }
        }

        private void ElectronicsRadioBtn_CheckedChanged(object sender, EventArgs e)
        {
            ElectronicsGroupBx.Enabled = true;
            LampsGroupBx.Enabled = false;
        }

        private void LampsButtonBtn_CheckedChanged(object sender, EventArgs e)
        {
                    
            ElectronicsGroupBx.Enabled = false;
            LampsGroupBx.Enabled = true;
        }

        private void DeleteConsumerBtn_Click(object sender, EventArgs e)
        {
            if(ElectricsListBx.Rows.Count > 0 && ElectricsListBx.SelectedRows[0].Index < ElectricsListBx.Rows.Count)
            {
                ElectricsListBx.Rows.RemoveAt(ElectricsListBx.SelectedRows[0].Index); 
            }
        }

        private void CalculateBtn_Click(object sender, EventArgs e)
        {
            var ElectroDataList = new List<ElectroData>();
            foreach (DataGridViewRow dRow in ElectricsListBx.Rows)
            {
                bool IsLamp = false;
                if (dRow.Cells[3].Value.ToString().Equals("l"))
                    IsLamp = true;

                for (int iter = 0; iter < int.Parse(dRow.Cells[1].Value.ToString()); iter++)
                {
                    ElectroDataList.Add(new ElectroData(dRow.Cells[0].Value.ToString(),
                        int.Parse(dRow.Cells[2].Value.ToString()),
                        IsLamp, (int?)dRow.Cells[4].Value,
                        float.Parse(dRow.Cells[5].Value.ToString())));
                }
            }

            int coreCount = 1;
            foreach (var control in CoreCountGroupBx.Controls)
            {
                if(control is RadioButton && ((RadioButton)control).Checked)
                {
                    coreCount = int.Parse(((RadioButton)control).Text);
                }
            }

            var cableData = new WireData((int)WireSectionСomboBx.SelectedValue, int.Parse(WireTypeComboBx.SelectedValue.ToString()),coreCount);
            var pcalc = new PowerCalculator(ElectroDataList, cableData, (int)(FloorComboBx.SelectedItem), (int)EntranceComboBx.SelectedItem);

            var outputForm = new OutputForm(pcalc);
            outputForm.ShowDialog();
        }
        
        void ShowSectionsByMaterial(int materialId)
        {
            var dictionaryWireSections = new Dictionary<int, string>();
            ElectricsRepository.GetSectionsByMaterialId(materialId).ToList().ForEach(wiresec => dictionaryWireSections.Add(wiresec.SectionId,
                                                                                                 (string.Format("{0} мм", wiresec.SectionValue))));
            WireSectionСomboBx.DataSource = dictionaryWireSections.ToArray();
            WireSectionСomboBx.DisplayMember = "Value";
            WireSectionСomboBx.ValueMember = "Key";
            WireSectionСomboBx.SelectedItem = WireSectionСomboBx.Items[0];
        }

        private void WireTypeComboBx_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(((ComboBox)sender).SelectedItem.ToString().Contains("Алюминий"))
            {
                ShowSectionsByMaterial(2);
            } 
            else if(((ComboBox)sender).SelectedItem.ToString().Contains("Медь"))
            {
                ShowSectionsByMaterial(1);
            }
        }

        private void Core1RadioBtnChanged(object sender, EventArgs e)
        {

        }






    }
}
